package com.chaihu.jpaDemo.common.hander;

import com.chaihu.jpaDemo.common.exception.RestException;
import com.chaihu.jpaDemo.common.exception.TimeOutException;
import com.chaihu.jpaDemo.modules.common.dto.ResultObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.nio.file.AccessDeniedException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 接口异常统一处理
 * 抛出exception执行exceptionHandler,
 * 自定义异常信息可以单独添加
 * @author shixh
 */
@ControllerAdvice(annotations = RestController.class)
public class BussinessExceptionHandler {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@ExceptionHandler	
	@ResponseBody
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	private ResultObject runtimeExceptionHandler(RuntimeException e) {
		logger.error("runtimeExceptionHandler error!", e);
		return ResultObject.error(e.getMessage());
	}

	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	private ResultObject illegalParamsExceptionHandler(MethodArgumentNotValidException e) {
		logger.error("illegalParamsExceptionHandler error!", e);
		return ResultObject.error(e.getMessage());
	}
	
	@ExceptionHandler(NullPointerException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	private ResultObject nullPointerExceptionExceptionHandler(NullPointerException e) {
		logger.error("nullPointerExceptionExceptionHandler error!", e);
		return ResultObject.error("空指针异常");
	}
	
	
	@ExceptionHandler(AccessDeniedException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	private ResultObject accessDeniedExceptionHandler(AccessDeniedException e) {
		logger.error("AccessDeniedException error!", e);
		return ResultObject.unAuthorized();
	}
	
	@ExceptionHandler(Exception.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	private ResultObject exceptionHandler(Exception e) {
		logger.error("exceptionHandler error!", e);
		return ResultObject.error("操作失败");
	}

	@ExceptionHandler(RestException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	private ResultObject bussinessExceptionHandler(RestException e) {
		logger.info("bussinessExceptionHandler error!");
		return ResultObject.error(e.getMessage());
	}


	/**
	 * validation 数据校验错误信息返回
	 * @param e
	 * @return
	 */
	@ExceptionHandler(ConstraintViolationException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public ResultObject constraintViolationExceptionHandler(ConstraintViolationException e){
		List<String> msglist = new ArrayList<>();
		Set<ConstraintViolation<?>> cves = e.getConstraintViolations();
		for (ConstraintViolation<?> constraintViolation : cves) {
			msglist.add(constraintViolation.getMessage());
		}
		return ResultObject.error(StringUtils.join(msglist, "|"));
	}
	
	@ExceptionHandler(TimeOutException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	private ResultObject unauthorizedExceptionHandler(TimeOutException e) {
		logger.error("UnauthorizedException error!", e);
		return ResultObject.unAuthorized("请重新登录！",e.getMessage());
	}
}
